import { createSlice } from '@reduxjs/toolkit'

export const userslice = createSlice({
    name: 'counter',
    initialState: {
        perlist: [
            {
                id: 1,
                code: '340116789098765432',
                status: false,
                name: '郑子豪',
                type:'成人票',
                phone:'18562145523'
            },
            {
                id: 2,
                code: '234456789098765432',
                status: false,
                name: '李四',
                 type:'儿童票',
                phone:'11522145523'
            },
            {
                id: 3,
                code: '562156789098765432',
                status: false,
                name: '张三',
                 type:'学生票',
                phone:'15822145523'
            },
        ]
    },
    reducers: {
        // 修改乘车人状态
        changestatus(state, { payload }) {
            state.perlist.forEach((item, index) => {
                if (item.id == payload.id) {
                    state.perlist[index].status =!payload.status
                    return
                }
            })
        },
        // 删除乘车人
        delper(state,{payload}){
            state.perlist=state.perlist.filter((item)=>{
                return item.id!=payload.id
            })
        },
         // 修改乘车人
          editper(state, { payload }) {
            console.log('pal',payload.id)
            state.perlist.map((item, index) => {
                if (item.id == payload.id) {
                 return   state.perlist[index] = payload
                }
            })
        },
          // 添加乘车人
        addper(state,{payload}){
            let c=0
            state.perlist.map((item,index)=>{
                if(item.id==payload.id){
                  return  c++
                }
            })
            if(c==0){
                state.perlist.push(payload)
            }
        }
    }
})
// 每个 case reducer 函数会生成对应的 Action creators
export const { changestatus,delper,editper,addper} = userslice.actions

export default userslice.reducer